//
//  SeasonSecondViewController.m
//  App-MyFairldy
//
//  Created by dlios on 15-5-26.
//  Copyright (c) 2015年 客官大大. All rights reserved.
//

#import "SeasonSecondViewController.h"

@interface SeasonSecondViewController ()

@end

@implementation SeasonSecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor blackColor];
    [self.view addSubview:activity];
    [activity startAnimating];
    activity.center = self.view.center;
    [self downLoadThisPageData];
    
}

- (void)initSpringTableView
{
    summerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 375, 667 - 64) style:UITableViewStylePlain];
    summerTableView.delegate = self;
    summerTableView.dataSource = self;
    [self.view addSubview:summerTableView];
    [summerTableView release];
    
}

- (void)downLoadThisPageData
{
    // 创建一个model
    NSString *str = @"http://food.boohee.com/fb/v1/foods?value=2&order_by=1&kind=season&order_asc=0";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array1 = [dic objectForKey:@"foods"];
        self.arraySeason = [NSMutableArray array];
        for (NSDictionary *dic1 in array1) {
            SeasonModel *model = [SeasonModel getseasonModel];
            model.name = [dic1 objectForKey:@"name"];
            model.calorie = [dic1 objectForKey:@"calory"];
            model.IconImage = [dic1 objectForKey:@"thumb_image_url"];
            [self.arraySeason addObject:model];
        };
        [self initSpringTableView];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    SeasonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[SeasonTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    SeasonModel *model = [self.arraySeason objectAtIndex:indexPath.row];
    cell.name.text = model.name;
    cell.calorie.text = model.calorie;
    
    NSURL *url = [NSURL URLWithString:model.IconImage];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    //    cell.imageView.image = image;
    cell.IconImage.image = image;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arraySeason.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
